package avriServer.command.avriServer;

import java.util.HashMap;

import avriServer.client.NonexistantWaiterException;
import avriServer.client.UnvalidWaiterException;
import avriServer.client.Waiter;
import avriServer.client.Worker;
import avriServer.command.Command;
import avriServer.command.WaiterBuilder;
import avriServer.json.JSONException;
import avriServer.json.JSONObject;
import avriServer.server.Dispatcher;
import avriServer.util.IdentificationException;

public class Ping extends Command {

	public Ping(JSONObject _jsonObject, HashMap<String, String> _attributes ) throws IdentificationException, NonexistantWaiterException {
		super(_jsonObject, _attributes);
	}

	@Override
	public void execute(Worker _worker) {
		String userToPing = this.getCommandAttribute("userToPing");
		//simple ping command, just ping the server.
		if (userToPing == null) {
			this.push("avriServer.pong", this.getCommandAttribute("pingTime")); 
		} else {
			try {
				Dispatcher.pushToWaiter(userToPing, "avriServer.pingFromServer", this.getCommandAttribute("pingTime"), this.getAttribute("name"), System.currentTimeMillis());
			} catch (NonexistantWaiterException e) {
				this.getWaiter().push("avriServer.error", "can't find this user");
			}
		}
	}
}
